package entity.lianxi.neixin;
import entity.test09.demo03.Person;

import java.beans.*;
import java.lang.reflect.Method;

public class DbutBean {

    public static void setProperty(Object bean, String propertyName, Object value) throws Exception {
        PropertyDescriptor descriptor = new PropertyDescriptor(propertyName, bean.getClass());
        Method writeMethod = descriptor.getWriteMethod();
        writeMethod.invoke(bean);
    }
    public static Object getProperty(Object bean, String propertyName) throws Exception {
        PropertyDescriptor descriptor = new PropertyDescriptor(propertyName, bean.getClass());
        return descriptor.getReadMethod().invoke(bean);
    }
    public static void copyProperties(Object sourceBean, Object targetBean)throws Exception {
        BeanInfo info = Introspector.getBeanInfo(sourceBean.getClass(), Object.class);

        PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            String name = propertyDescriptor.getName();
            Object property = getProperty(sourceBean, name);
            setProperty(targetBean,name,property);

        }
    }

    public static void main(String[] args) throws Exception {
        Person person = new Person();
        setProperty(person, "name", "张三");
        setProperty(person, "age", 30);

        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}
